<template>
    <div>
        <el-card class="box-card" shadow="never">
            <i class="el-icon-search"></i>
            <span>筛选搜索</span>
            <el-row :gutter="10" style="margin-top:15px">
               <el-col :span='6'>
                  <el-input v-model="realname">
                    <template slot="prepend">姓名</template>
                  </el-input>
               </el-col>
               <el-col :span='6'>
                  <el-select v-model="if_identify" placeholder="人证比对状态" clearable>
                      <el-option label="未比对" value="0"></el-option>
                      <el-option label="人证相符" value="1"></el-option>
                   </el-select>
               </el-col>
               <el-col :span='6'>
                  <div class="block">
                    <el-cascader
                      v-model="user_type"
                      :options="allusertype"
                      :props="props"
                      placeholder="优抚对象类别"
                      collapse-tags
                      style="width:100%"
                      clearable></el-cascader>
                  </div>
               </el-col>
            </el-row>
            <el-row class="yx_el_row">
                <el-col>
                    <el-button type="success" plain @click="search">筛选</el-button>
                    <el-button type="primary" plain @click="add">添加</el-button>
                    <el-button type="primary" plain :loading="excel_loading" @click="exportExcel">{{excel_button}}</el-button>
                    <ExcelImport style="display:inline-block;margin-left:10px;" :post_url="excel_post_url" :show_file_list="show_file_list" :accept_str="accept_str" :handle_result="handle_result"></ExcelImport>
                    <el-link type="success" @click="download">模板下载</el-link>
                </el-col>
            </el-row>
            <div class="yx_line"></div>
            <div style="margin-top:10px;">
              <i class="el-icon-tickets"></i>
              <span>数据列表</span>
            </div>
            <el-table class="yx_table" border :data="userlist" v-viewer="{movable: false}">
                <el-table-column type="index" label="序号" width="100">
                </el-table-column>
                <el-table-column prop="photo" label="照片" width="180">
                    <template slot-scope="scope">
                          <el-image
                              style="width: 100px;"
                              :src="scope.row.photo"
                              @click="showBig"
                          fit="fill">
                          <div slot="error" class="image-slot">
                             <i class="el-icon-picture-outline"></i>
                          </div>
                          </el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="realname" label="姓名" width="100">
                </el-table-column>
                <el-table-column prop="id_card" label="身份证号" width="200">
                </el-table-column>
                <el-table-column prop="bank_no" label="银行卡号" width="200">
                </el-table-column>
                <el-table-column prop="age" label="年龄" width="80">
                </el-table-column>
                <el-table-column prop="nation" label="民族" width="80">
                </el-table-column>
                <el-table-column prop="user_type" label="优抚对象类别" width="180">
                </el-table-column>
                <el-table-column prop="extend_amount" label="抚恤金额/月" width="100">
                </el-table-column>
                <el-table-column prop="if_identify" label="人证比对状态" width="150">
                </el-table-column>
                <el-table-column  label="操作">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="dark" content="编辑" placement="bottom-start">
                            <el-button type="primary" icon="el-icon-edit" @click="edit(scope.row.id,2)" circle></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="查看"  placement="bottom-start">
                            <el-button type="info" icon="el-icon-notebook-1" @click="edit(scope.row.id,0)" circle></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="删除" placement="bottom-start">
                            <el-button type="warning" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination :total_count="total_count" :getList="getList"></Pagination>
        </el-card>
    </div>
</template>

<script>
    import { getUserList,delUser } from '@/api/user.js'
    import ExcelImport from '@/components/ExcelImport.vue';
    import Pagination from '@/components/Pagination.vue';
    export default {
        name: 'UserList',
        components:{ ExcelImport,Pagination },
        inject:['reload'],
        data() {
            return {
                showDialog:false,
                button_name:'立即设置',
                loading:false,
                realname:'',
                if_identify:'',
                user_type:[],
                //导入excel目标路径
                excel_post_url:'/importuserlist',
                show_file_list:false,
                accept_str:".xls,.xlsx",
                allusertype:this.$store.state.allusertype,
                allchildtype:this.$store.state.allchildtype,
                props: { multiple: true },
                excel_loading:false,
                excel_button:'导出Excel',
                userlist:[],
                total_count:0,
            }
        },
        methods: {
            getList(current_page,page_size){
                this.$store.dispatch('loadCurrentPageSize',page_size);
                var send_data={
                    current_page:current_page,
                    page_size:page_size,
                    realname:this.realname,
                    if_identify:this.if_identify==''?-1:this.if_identify,
                    user_type:JSON.stringify(this.user_type),
                    type:'list'
                }
                let that=this;
                getUserList(send_data).then(function(response){
                    if(response.data.code==1){
                        that.userlist=response.data.list;
                        that.total_count=response.data.total_count;
                    }
                })
            },
            search(){
               this.getList(1,this.$store.getters.getCurrentPageSize);
            },
            add(){
               this.$router.push({
                   name:'useredit',
                   params:{id:-1,edit_type:1}
               });
            },
            edit(id,edit_type){
                this.$router.push({
                    name:'useredit',
                    params:{id:id,edit_type:edit_type}
                });
            },
            del(id){
                let that=this;
                //弹框提示
                this.$confirm('确认删除, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then((e) => {
                    var send_data={
                        id:id,
                        is_delete:1
                    };
                    delUser(send_data).then(function(response){
                        let code=response.data.code;
                        let type=code==1?'success':'error';
                        that.$message({
                            message:response.data.msg,
                            type:type,
                            onClose:function(e){
                              if(code==1){
                                 that.reload();
                              }
                            }
                        });
                    })
                })
            },
            //处理excel导入的结果
            handle_result(res){
               let that=this;
               let type=res.code==1?'success':'error';
               this.$message({
                   message:res.msg,
                   type:type,
                   onClose:function(e){
                      if(res.code==1){
                        that.reload();
                      }
                   }
               });
            },
            formatJson(filterVal, jsonData) {
                 return jsonData.map(v => filterVal.map(j => v[j]));
            },
            exportExcel(){
                this.excel_loading=true;
                this.excel_button="正在导出";
                var send_data={
                    current_page:1,
                    page_size:20,
                    realname:this.realname,
                    if_identify:this.if_identify==''?-1:this.if_identify,
                    user_type:JSON.stringify(this.user_type),
                    type:'excel'
                }
                let that=this;
                getUserList(send_data).then(function(response){
                    if(response.data.code==1){
                        if(response.data.list.length>0){
                            const { export_json_to_excel } = require('../../api/excel/export2excel.js');
                            const tHeader = ["姓名","身份证号","银行卡号","年龄","民族","优抚对象类别","抚恤金额/月","认证对比状态"];
                            const fields = ["realname","id_card","bank_no","age","nation","user_type","extend_amount","if_identify"];
                            const list=response.data.list;
                            const data=that.formatJson(fields,list);
                            let date=new Date();
                            let now_date=date.getFullYear().toString()+(date.getMonth()+1).toString()+date.getDate().toString();
                            export_json_to_excel(tHeader,data,"用户中心-"+now_date);
                        }else{
                            that.$message({
                                message:'没有可导出数据',
                                type:'warning',
                            });
                        }
                        that.excel_loading=false;
                        that.excel_button_name='导出Excel';
                    }
                })
            },
            //图片放大
            showBig(){
               const viewer = this.$el.querySelector('.yx_table').$viewer
               viewer.show();
            },
            //模板下载
            download(){
                window.open('https://retire.wshoto.com/uploads/用户信息导入模板.xlsx');
            }
        },
        created() {
            //将childtype添加到伤残人员后面
            this.allusertype[0]['children']=this.allchildtype;
            this.getList(1,this.$store.state.default_page_size);
        }
    }
</script>

<style>
     .el-image{
       hover:pointer;
       cursor: pointer;
     }
</style>
